package redisutil

import (
	"context"
	"fmt"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/redislib"
)

/*
登录服的角色数据，提供给游戏服务器获取使用
*/
type LoginPlayer struct {
	key string
	//获取redis操作的方法
	getRedisF func() *redislib.RedisHandleModel
}

func NewLoginPlayer(pid uint64) *LoginPlayer {
	return &LoginPlayer{
		key:       fmt.Sprintf("PlayerInfo:%d", pid),
		getRedisF: global.GetRedisAccess(global.RedisName_Login).GetConn,
	}
}

const (
	LoginPlayer_Key_PlayerID   = "playerid"
	LoginPlayer_Key_GroupID    = "groupid"
	LoginPlayer_Key_UserID     = "userid"
	LoginPlayer_Key_PlayerName = "playername"
)

func (this *LoginPlayer) SaveInfo(v map[string]any) error {
	rd := this.getRedisF()
	defer rd.Expire(context.Background(), this.key, global.Expire3Day)
	return rd.HMSet(context.Background(), this.key, v).Err()
}
func (this *LoginPlayer) GetInfo() (common.GMap, error) {
	rd := this.getRedisF()
	return rd.HGetAll(context.Background(), this.key).Result()
}

// 刷新过期时间
func (this *LoginPlayer) Expire() {
	c := this.getRedisF()
	c.Expire(c.Ctx, this.key, global.Expire3Day)
}
